<template>
	<div class="file-upload" :style="{width,height}">
		<div v-if="value" class="upload-image">
			<img :src="value" />
			<span class="delete-image" @click="deleteValue()">x</span>
		</div>
		<div v-else class="upload-btn" @click="fileUpload">
			<template v-if="isLoading">
				<span class="ant-spin-dot ant-spin-dot-spin">
					<i class="ant-spin-dot-item"></i>
					<i class="ant-spin-dot-item"></i>
					<i class="ant-spin-dot-item"></i>
					<i class="ant-spin-dot-item"></i>
				</span>
				<p>上传中...</p>
			</template>
			<template v-else>
				<span>+</span>
				<p>点击上传</p>
			</template>
		</div>
	</div>
</template>
<script>
import request from "@/util/httpUtils";
import { message } from "ant-design-vue";
export default {
	name: "avatarUpload",
	props: {
		value: {
			type: String,
			default: "",
		},
		config: {
			type: Object,
			default: null,
		},
		width:{
			type:String,
			default:"104px",
		},
		height:{
			type:String,
			default:"104px"
		}
	},
	data() {
		return {
			input: null,
			isLoading: false,
		};
	},
	mounted() {},
	methods: {
		deleteValue() {
			this.$emit("update:value", "");
		},
		fileUpload() {
			this.input = document.createElement("input");
			this.input.type = "file";
			this.input.id = "upload-file";
			this.input.style.position = "fixed";
			this.input.style.left = "-100%";
			this.input.style.right = "-100%";
			this.input.onchange = this.upload;
			document.body.appendChild(this.input);
			this.input.dispatchEvent(new MouseEvent("click"));
		},
		upload(e) {
			const config = {
				baseURL: request.defaults.baseURL,
				url: "/Admin/Api/uploadFile",
				size: 4096,
				name: "file",
				fileType: ".png,.jpg,.jpeg",
			};
			const conf = this.config ? { ...this.config, ...config } : { ...config };
			const file = e.target.files[0];
			const { name, size } = file;
			const fileType = name.slice(name.lastIndexOf("."));
			if (conf.fileType !== "all" && conf.fileType.indexOf(fileType) === -1) {
				message.error("上传文件格式不正确");
				return;
			}
			if (size > conf.size * 1024) {
				message.error("图片大小超过限制,不能上传超过" + conf.size + "KB的文件");
				return;
			}
			const formData = new FormData();
			const xhr = new XMLHttpRequest();
			formData.append(conf.name, file);
			xhr.onreadystatechange = () => {
				if (xhr.readyState === 4) {
					if (xhr.status === 200) {
						const data = JSON.parse(xhr.response);
						this.$emit("update:value", data.data.fileUrl);
					} else {
						e.target.value = null;
					}
					this.input.remove();
					this.isLoading = false;
				}
			};
			xhr.open("POST", conf.baseURL + conf.url, true);
			this.isLoading = true;
			let token = sessionStorage.getItem("token");
			if (token) {
				xhr.setRequestHeader("Authorization", "Bearer " + token);
			}
			xhr.send(formData);
		},
	},
};
</script>
<style scoped lang="less">
.file-upload {
	width: 104px;
	height: 104px;
	position: relative;
}
.upload-image {
	width: 100%;
	height: 100%;
	display: block;
	position: relative;
	background-color: #f6f6f6;
	> img {
		width: 100%;
		height: 100%;
		display: block;
		border-radius: 8px;
	}
	.delete-image {
		position: absolute;
		right: 0px;
		top: 0px;
		width: 20px;
		height: 20px;
		line-height: 19px;
		text-align: center;
		font-size: 14px;
		cursor: pointer;
		color: #fff;
		background: rgba(0, 0, 0, 0.3);
		&:hover {
			background: rgba(0, 0, 0, 0.5);
		}
	}
}
.upload-btn {
	width: 100%;
	height: 100%;
	margin-right: 8px;
	margin-bottom: 8px;
	text-align: center;
	vertical-align: top;
	background-color: #fafafa;
	border: 1px dashed #d9d9d9;
	border-radius: 8px;
	cursor: pointer;
	transition: border-color 0.3s ease;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	cursor: pointer;
	&:hover {
		border-color: #1890ff;
	}
	>span {
		font-size: 24px;
		color: #777;
		line-height: 18px;
		margin-bottom: 5px;
		&.ant-spin-dot{
			line-height: normal;
			margin-bottom: 10px;
		}
	}
	p {
		margin-bottom: 0;
		color: #666;
	}
}
.ant-spin-dot-spin {
	transform: rotate(45deg);
	animation: antRotate 1.2s infinite linear;
}
.ant-spin-dot {
    position: relative;
    display: inline-block;
    font-size: 20px;
    width: 1em;
    height: 1em;
}

.ant-spin-dot-item {
    position: absolute;
    display: block;
    width: 9px;
    height: 9px;
    background-color: #1890ff;
    border-radius: 100%;
    transform: scale(.75);
    transform-origin: 50% 50%;
    opacity: .3;
    animation: antSpinMove 1s infinite linear alternate
}

.ant-spin-dot-item:nth-child(1) {
    top: 0;
    left: 0
}

.ant-spin-dot-item:nth-child(2) {
    top: 0;
    right: 0;
    animation-delay: .4s
}

.ant-spin-dot-item:nth-child(3) {
    right: 0;
    bottom: 0;
    animation-delay: .8s
}

.ant-spin-dot-item:nth-child(4) {
    bottom: 0;
    left: 0;
    animation-delay: 1.2s
}

.ant-spin-dot-spin {
    transform: rotate(45deg);
    animation: antRotate 1.2s infinite linear
}
@keyframes antSpinMove {
    to {
        opacity: 1
    }
}
@keyframes antRotate {
    to {
        transform: rotate(405deg)
    }
}
</style>
